package cn.stylefeng.guns.sys.modular.system.db4.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.db4.entity.BizShop;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.BizShopMapper;
import cn.stylefeng.guns.sys.modular.system.db4.model.params.BizShopParam;
import cn.stylefeng.guns.sys.modular.system.db4.model.result.BizShopResult;
import cn.stylefeng.guns.sys.modular.system.db4.model.result.ShopOwnerInfoResult;
import cn.stylefeng.guns.sys.modular.system.db4.service.BizShopService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店信息表 服务实现类
 * </p>
 *
 * @author yxc
 * @since 2021-05-10
 */
@Service
public class BizShopServiceImpl extends ServiceImpl<BizShopMapper, BizShop> implements BizShopService {

    @Override
    public void add(BizShopParam param){
        BizShop entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BizShopParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BizShopParam param){
        BizShop oldEntity = getOldEntity(param);
        BizShop newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BizShopResult findBySpec(BizShopParam param){
        return null;
    }

    @Override
    public List<BizShopResult> findListBySpec(BizShopParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BizShopParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BizShopParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BizShop getOldEntity(BizShopParam param) {
        return this.getById(getKey(param));
    }

    private BizShop getEntity(BizShopParam param) {
        BizShop entity = new BizShop();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 获取店长信息
     * @return
     */
    @Override
    public List<Map> getDzlist() {
        return this.baseMapper.getDzlist();
    }

    @Override
    public  Map<String,ShopOwnerInfoResult>  shopOwnerInfoList() {
        List<ShopOwnerInfoResult> infoList=this.baseMapper.shopOwnerInfoList();

        Map<String,ShopOwnerInfoResult> map=new HashMap<>();

        if(CollectionUtil.isNotEmpty(infoList)){
            for(ShopOwnerInfoResult result:infoList){
                if(result.getYaoma()!=null&&!result.getYaoma().equals("")){
                    map.put(result.getYaoma(),result);
                }
            }
        }


        return map;
    }
}
